/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.infosystem.client.business;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import ru.infosystem.client.data.Faculty;
import ru.infosystem.client.data.Group;
import ru.infosystem.client.data.Message;
import ru.infosystem.client.data.ServerException;
import ru.infosystem.client.data.Student;
import ru.infosystem.client.task.Task;
import ru.infosystem.server.dao.DaoFactory;
import ru.infosystem.server.dao.DaoFaculty;
import ru.infosystem.server.dao.DaoGroup;
import ru.infosystem.server.dao.DaoStudent;
import ru.infosystem.server.dao.xml.XmlDaoFactory;

/**
 *
 * @author DukeKan
 */
public class TaskAnalyser {

    private Task task;
    private ObjectInputStream in;
    private ObjectOutputStream out;

    public TaskAnalyser(ObjectInputStream in, ObjectOutputStream out) {
        this.in = in;
        this.out = out;
    }

    public void runTask() throws NoSuchMethodException {
        try {
            this.task = (Task) in.readObject();
        } catch (IOException ex) {
            Logger.getLogger(TaskAnalyser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(TaskAnalyser.class.getName()).log(Level.SEVERE, null, ex);
        }
        switch (task.command) {
            case ADD_STUDENT:
                System.out.println("add student");
                this.addStudent();

                break;
            case DELETE_STUDENT:
                System.out.println("delete student");

                this.deleteStudent();
                break;
            case DELETE_GROUP:
                System.out.println("delete group");

                this.deleteGroup();
                break;
            case ADD_GROUP:
                System.out.println("add group");

                this.addGroup();
                break;
            case LIST_STUDENTS_OF_GROUP:
                System.out.println("list student");

                this.listStudentsOfGroup();
                break;
            case LIST_GROUPS_OF_FACULTY:
                System.out.println("list groups");

                this.listGroupsOfFaculty();
                break;
            case FIND:
                System.out.println("find");
                this.find();
                break;

        }
        try {
            this.out.flush();
            this.in.skipBytes(in.available());
        } catch (IOException ex) {
            Logger.getLogger(TaskAnalyser.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void addStudent() {
        DaoFactory daoFactory = new XmlDaoFactory();
        DaoStudent student = daoFactory.getStudent();
        boolean done = false;
        try {
            done = student.addStudent((Student) this.task.object);
        } catch (ServerException n) {
            try {
                out.writeObject(n);
            } catch (IOException ex) {
                System.err.println("Не удалось отправить исключение");
            }
        }
        try {
            out.writeObject(new Boolean(done));
            //out.writeBoolean(done);
        } catch (IOException ex) {
            Logger.getLogger(TaskAnalyser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void deleteStudent() throws NoSuchMethodException {
        DaoFactory daoFactory = new XmlDaoFactory();
        DaoStudent student = daoFactory.getStudent();
        boolean done = student.deleteStudent((Student) this.task.object);
        System.out.println("deleted " + done);
        try {
            out.writeObject(new Boolean(done));
        } catch (ServerException n) {
            try {
                out.writeObject(n);
            } catch (IOException ex) {
                System.err.println("Не удалось отправить исключение");
            }
        } catch (IOException ex) {
            Logger.getLogger(TaskAnalyser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void addGroup() throws NoSuchMethodException {
        DaoFactory daoFactory = new XmlDaoFactory();
        DaoGroup group = daoFactory.getGroup();
        try {
            boolean done = group.addNewGroup((Group) this.task.object);
            out.writeObject(new Boolean(done));
        } catch (ServerException n) {
            try {
                out.writeObject(n);
            } catch (IOException ex) {
                System.err.println("Не удалось отправить исключение");
            }
        } catch (IOException ex) {
            Logger.getLogger(TaskAnalyser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void deleteGroup() throws NoSuchMethodException {
        DaoFactory daoFactory = new XmlDaoFactory();
        DaoGroup group = daoFactory.getGroup();
        boolean done = false;
        try {
            done = group.deleteGroup((Group) this.task.object);
            this.out.writeObject(new Boolean(done));
        } catch (ServerException n) {
            try {
                out.writeObject(n);
                //out.writeObject(n);
            } catch (IOException ex) {
                System.err.println("Не удалось отправить исключение");
            }
        } catch (IOException ex) {
            Logger.getLogger(TaskAnalyser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void listStudentsOfGroup() throws NoSuchMethodException {
        DaoFactory daoFactory = new XmlDaoFactory();
        DaoGroup group = daoFactory.getGroup();
        try {
            ArrayList<Student> l = group.getListOfStudents(((Group) this.task.object).getGroupNumber());
            out.writeObject(l);
        } catch (ServerException n) {
            try {
                out.writeObject(n);
            } catch (IOException ex) {
                System.err.println("Не удалось отправить исключение");
            }
        } catch (IOException ex) {
            Logger.getLogger(TaskAnalyser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void listGroupsOfFaculty() throws NoSuchMethodException {
        DaoFactory daoFactory = new XmlDaoFactory();
        DaoFaculty faculty = daoFactory.getFaculty();
        try {
            out.writeObject(faculty.getListOfGroups(((Faculty) this.task.object).getNum()));
        } catch (ServerException n) {
            try {
                out.writeObject(n);
            } catch (IOException ex) {
                System.err.println("Не удалось отправить исключение");
            }
        } catch (IOException ex) {
            Logger.getLogger(TaskAnalyser.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void find() {
        DaoFactory daoFactory = new XmlDaoFactory();
        DaoFaculty faculty = daoFactory.getFaculty();
        try {
            out.writeObject(faculty.getListOfMathesStudents(((Message) this.task.object).message));
        } catch (ServerException n) {
            try {
                out.writeObject(n);
            } catch (IOException ex) {
                System.err.println("Не удалось отправить исключение");
            }
        } catch (IOException ex) {
            Logger.getLogger(TaskAnalyser.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
